package gui_classes;

import java_persistence.AppointmentMapper;
import java_persistence.DoctorMapper;
import java_persistence.NurseMapper;
import java_persistence.PatientMapper;
import java_persistence.TreatmentRecordMapper;
import java_persistence.UserMapper;

import javax.swing.*;

import domain.Appointment;
import domain.Doctor;
import domain.Patient;

import java.awt.*;
import java.awt.event.*;

import java.util.Iterator;
import java.util.List;


/**
 * This panel displays data relevant to patients
 */
public class PatientsPageTwoPanel extends JPanel {
	
	private Patient currPatient;
	private JComboBox comboBox;



	/**
	 * Create the panel.
	 */
	public PatientsPageTwoPanel(Patient pat) {

		setPreferredSize(new Dimension(300, 300));
		setLayout(null);
		
		this.currPatient = pat;
		
		
		JButton btnRequestAppointment = new JButton("Request Appointment");
		btnRequestAppointment.setBounds(50, 77, 198, 25);
		btnRequestAppointment.addActionListener(new AppointmentListener());
		add(btnRequestAppointment);
		
		JButton btnView = new JButton("View");
		btnView.setBounds(89, 234, 117, 25);
		btnView.addActionListener(new ViewListener());
		add(btnView);
		
		List<String> apps = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(this.currPatient.getUsername());

		int i=0;
		String[] appStr = new String[apps.size()];
			for ( Iterator<String> appsIter = apps.iterator(); appsIter.hasNext(); ) {
				String cura = appsIter.next();
				appStr[i]=cura;
				i++;
			}
		comboBox = new JComboBox(appStr); //appStr
		comboBox.setBounds(67, 146, 166, 25);
		add(comboBox);
		
		JLabel lblViewMedicalHistory = new JLabel("View Medical History");
		lblViewMedicalHistory.setBounds(82, 119, 166, 15);
		add(lblViewMedicalHistory);

	}
	
	public Patient getPatient()
	{
		return this.currPatient;
	}
	
	public JComboBox getComboBox()
	{
		return this.comboBox;
	}
	
	/*
	 * Listener for Appointment Request Button	
	 */
	private class AppointmentListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			System.out.println("Patient wants to request appointment");
			// who is currPatient?
			System.out.println(GUI_Main.ppage.getPanelTwo().getPatient().getUsername());
			GUI_Main.appointpage = new PatientAppointmentRequestPanel(GUI_Main.ppage.getPanelTwo().getPatient());
			GUI_Main.appointpage.setPrevious(GUI_Main.ppage);
			
			GUI_Main.appointpage.display();
		//	GUI_Main.replacePanel(GUI_Main.frame, GUI_Main.appointpage);
			
		}
	}
	
	private class ViewListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			System.out.println("Do somethign");
			GUI_Main.mscreen = new MedicalHistoryPage();
			try
			{
			String date = (String) GUI_Main.ppage.getPanelTwo().getComboBox().getSelectedItem();
			String pu = GUI_Main.ppage.getPanelTwo().getPatient().getUsername();
			String doctor_username = GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_attending_doctor_username(date, pu);
			String nurse_username = GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_attending_nurse_username(date, pu);
			String diagnosis = GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_diagnosis(date, pu);
			String notes = GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_notes(date, pu);
			String bpd = ( GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_bp_diastolic(date, pu)==null) ? "" : GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_bp_diastolic(date, pu).toString();
			String bps = ( GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_bp_systolic(date, pu)==null) ? "" : GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_bp_systolic(date, pu).toString();
			String ht = ( GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_height_inches(date, pu)==null) ? "" : GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_height_inches(date, pu).toString();
			String pulse =( GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_pulse(date, pu)==null) ? "" : GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_pulse(date, pu).toString();
			String temp = ( GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_temp(date, pu)==null) ? "" : GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_temp(date, pu).toString();
			String wt = ( GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_weight_pounds(date, pu)==null) ? "" :   GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_weight_pounds(date, pu).toString();

			GUI_Main.mscreen.setDate(date);
			GUI_Main.mscreen.setFName(GUI_Main.getSession().getMapper(UserMapper.class).get_user_by_username(pu).getName());
			GUI_Main.mscreen.setDoctor(GUI_Main.getSession().getMapper(DoctorMapper.class).get_doctor_by_username(doctor_username).getName());
			GUI_Main.mscreen.setNurse(GUI_Main.getSession().getMapper(NurseMapper.class).get_nurse_by_username(nurse_username).getName());
			GUI_Main.mscreen.setDiagnosis(diagnosis);
			GUI_Main.mscreen.setNotes(notes);
			GUI_Main.mscreen.setBPD(bpd);
			GUI_Main.mscreen.setBPS(bps);
			GUI_Main.mscreen.setHeight(ht);
			GUI_Main.mscreen.setPulse(pulse);
			GUI_Main.mscreen.setTemp(temp);
			GUI_Main.mscreen.setWeight(wt);

			
			
			GUI_Main.mscreen.setPrevious(GUI_Main.ppage);
			GUI_Main.mscreen.display();
			}
			catch(NullPointerException e)
			{
				JOptionPane.showMessageDialog(null, "Human: You can only view medical history, not upcoming appointments. The dates are already on there.\nProgrammer:" + e.toString());
				e.printStackTrace();
			}
		}
	}
	
}
